package com.itheima.拓展练习;

import java.util.concurrent.CountDownLatch;

public class Test02 {
    public static void main(String[] args) {
        CountDownLatch cdl = new CountDownLatch(1);
        A a = new A(cdl);
        a.start();
        B b = new B(cdl);
        b.start();
    }
}
class A extends Thread{
    private CountDownLatch cdl;

    public A(CountDownLatch cdl) {
        this.cdl = cdl;
    }

    private int sum;
    @Override
    public void run() {
        for (int i = 1; i <= 100; i++) {
            sum+=i;
        }
        System.out.println("sum = " + sum);
        cdl.countDown();
    }
}
class B extends Thread{
    private CountDownLatch cdl;

    public B(CountDownLatch cdl) {
        this.cdl = cdl;
    }

    @Override
    public void run() {
        System.out.println("开始计算");
        try {
            cdl.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("计算完毕");
    }
}
